package com.codi.api.controller.apply;

import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.StringUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.bus.core.resultModel.ApplyListResult;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.resultModel.DividendApplyResult;
import com.codi.bus.core.resultModel.ValidateTokenResult;
import com.codi.bus.core.service.DividendApplyService;
import com.codi.bus.core.service.UserService;
import com.codi.message.util.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by asus on 2018/3/16.
 */
@Slf4j
@RestController
@RequestMapping("dividend/apply")
public class DividendApplyController {
    @Autowired
    private UserService userService;

    @Autowired
    private DividendApplyService dividendApplyQuery;


    /**
     * 组合 分红交易记录
     * @param token
     * @param requestNum
     * @param queryFlag
     * @param beginNum
     * @param sortDirection
     * @param deviceId
     * @return
     */
    @GetMapping("portfolio")
    @Loggable
    public BaseResult portfolioDividendApplyQuery(@RequestParam String token,
                                 @RequestParam Integer requestNum,
                                 @RequestParam Integer queryFlag,
                                 @RequestParam Integer beginNum,
                                 @RequestParam String sortDirection,
                                                  String allotNo,
                                 @RequestHeader(value = GlobalConstant.HEADER_APP_DEVICE) String deviceId) {
        DividendApplyResult result = null;
        // 验证token
        ValidateTokenResult validateTokenResult = userService.validateToken(token, deviceId);
        if (!validateTokenResult.getSuccess()) {
            String errorMessage = MessageUtil.getErrorMessage(validateTokenResult.getErrorCode(), null);
            validateTokenResult.setErrorMessage(errorMessage);
            return validateTokenResult;
        }

        result = dividendApplyQuery.queryPortfolioDividendApplys(validateTokenResult.getUserId(), requestNum, queryFlag, beginNum,
            sortDirection,allotNo);
        if (!result.getSuccess() && StringUtil.isEmpty(result.getErrorMessage())) {
            result.setErrorMessage(MessageUtil.getErrorMessage(result.getErrorCode(), null));
        }
        return result;
    }


    /**
     * 普通基金分红交易记录
     *
     * @param token
     * @param requestNum    请求行数
     * @param queryFlag     1
     * @param beginNum      开始行数
     * @param sortDirection 排序 1
     * @param deviceId      设备号
     * @return
     */
    @GetMapping("/fund")
    @Loggable
    public BaseResult fundDividendApplyQuery(@RequestParam String token,
                                             @RequestParam Integer requestNum,
                                             @RequestParam Integer queryFlag,
                                             @RequestParam Integer beginNum,
                                             @RequestParam String sortDirection,
                                             String allotNo,
                                             @RequestHeader(value = GlobalConstant.HEADER_APP_DEVICE) String deviceId) {
        DividendApplyResult result = null;
        // 验证token
        ValidateTokenResult validateTokenResult = userService.validateToken(token, deviceId);
        if (!validateTokenResult.getSuccess()) {
            String errorMessage = MessageUtil.getErrorMessage(validateTokenResult.getErrorCode(), null);
            validateTokenResult.setErrorMessage(errorMessage);
            return validateTokenResult;
        }
        result = dividendApplyQuery.queryNormalDividendApplys(validateTokenResult.getUserId(), requestNum, queryFlag, beginNum,
            sortDirection,allotNo);
        if (!result.getSuccess() && StringUtil.isEmpty(result.getErrorMessage())) {
            result.setErrorMessage(MessageUtil.getErrorMessage(result.getErrorCode(), null));
        }
        return result;
    }

    /**
     * 普通基金分红交易记录
     *
     * @param token
     * @param requestNum    请求行数
     * @param queryFlag     1
     * @param beginNum      开始行数
     * @param sortDirection 排序 1
     * @param deviceId      设备号
     * @return
     */
    @GetMapping("/wallet")
    @Loggable
    public BaseResult walletDividendApplyQuery(@RequestParam String token,
                                             @RequestParam Integer requestNum,
                                             @RequestParam Integer queryFlag,
                                             @RequestParam Integer beginNum,
                                             @RequestParam String sortDirection,
                                             String allotNo,
                                             @RequestHeader(value = GlobalConstant.HEADER_APP_DEVICE) String deviceId) {
        DividendApplyResult result = null;
        // 验证token
        ValidateTokenResult validateTokenResult = userService.validateToken(token, deviceId);
        if (!validateTokenResult.getSuccess()) {
            String errorMessage = MessageUtil.getErrorMessage(validateTokenResult.getErrorCode(), null);
            validateTokenResult.setErrorMessage(errorMessage);
            return validateTokenResult;
        }
        result = dividendApplyQuery.queryWalletDividendApplys(validateTokenResult.getUserId(), requestNum, queryFlag, beginNum,
            sortDirection,allotNo);
        if (!result.getSuccess() && StringUtil.isEmpty(result.getErrorMessage())) {
            result.setErrorMessage(MessageUtil.getErrorMessage(result.getErrorCode(), null));
        }
        return result;
    }
}
